package homework;

/**
 * 设计一个多线程的程序如下：设计一个火车售票模拟程序。
 * 假如火车站要有100张火车票要卖出，现在有5个售票点同时售票，用5个线程模拟这5个售票点的售票情况。
 */
class SaleTrain implements Runnable{

    private int saleTrain = 100;
    @Override
    public void run() {
        for (;;){
            synchronized (this){
                if (saleTrain > 0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+
                            "正在买票，剩余 "+saleTrain+"张票");
                    saleTrain--;
                }else {
                    System.out.println(Thread.currentThread().getName()+"对不起，票已售空！");
                    break;
                }
            }
        }
    }
}


public class No_5 {
    public static void main(String[] args) {
        SaleTrain st = new SaleTrain();
        new Thread(st,"窗口 A ").start();
        new Thread(st,"窗口 B ").start();
        new Thread(st,"窗口 C ").start();
        new Thread(st,"窗口 D ").start();
        new Thread(st,"窗口 E ").start();
    }




}
